import { Module } from 'vuex'
import type { IStoryState } from './types'
import type { IRootState } from '@/store/type'
import { getStoryList, setStoryList } from '@/service/main/story/story'

const storyModule: Module<IStoryState, IRootState> = {
  namespaced: true,
  state() {
    return {
      storyList: [],
      storyCount: 0
    }
  },
  mutations: {
    changeStoryList(state, storyList) {
      state.storyList = storyList
    },
    changeStoryCount(state, storyCount) {
      state.storyCount = storyCount
    }
  },
  actions: {
    async getPageListAction({ commit }, payload: any) {
      const pageName = payload.pageName

      const pageUrl = `/${pageName}/list`
      const pageRes = await getStoryList(pageUrl, payload.queryInfo)
      const { list, totalCount } = pageRes.data
      const changePageName =
        pageName.slice(0, 1).toUpperCase() + pageName.slice(1)
      commit(`change${changePageName}List`, list)
      commit(`change${changePageName}Count`, totalCount)
    },
    async setStoryList({ commit }, payload: any) {
      const pageName = payload.pageName
      const pageUrl = `/${pageName}`
      const storyRes = await setStoryList(pageUrl, payload.queryInfo)
    }
  },
  getters: {}
}

export default storyModule
